-- open_grid
-- created by zouyb
-- 开格子

local handle = function(para)
    -- 如果不是在地牢内就忽略
    if not DungeonM.isInDungeon() then
        return;
    end

    -- 开门/公开的格子的就不要管了
    if para.type == GRID_TYPE_DOOR or para.isPublicGrid then
        return;
    end

    -- 已翻开的格子数
    local addPoints = PropStatM.getData1(ME.user, "open_grid_bonus");
    addPoints = addPoints + 1;
    PropStatM.setData1(ME.user, "open_grid_bonus", addPoints);

    local fetchProps = PropM.fetchProps;
    local trigger = PropM.trigger;

    -- 触发prop
    local props = fetchProps(ME.user, "open_grid_bonus");
    for _, prop in ipairs(props) do
        trigger(ME.user, prop[1], prop[2]);
    end

    -- 开格子概率获得物品
    props = PropM.fetchProps(ME.user, "open_grid_probability");
    for _, prop in ipairs(props) do
        PropM.trigger(ME.user, prop[1], prop[2], para.pos);
    end

    -- 触发skill
    props = PropM.fetchProps(ME.user, "open_grid_skill");
    for _, prop in ipairs(props) do
        PropM.trigger(ME.user, prop[1], prop[2]);
    end

    -- 开格子加属性
    props = fetchProps(ME.user, "open_grid_recover");
    for _, prop in ipairs(props) do
        trigger(ME.user, prop[1], prop[2], para.pos);
    end

    -- 开格子加属性（本层有效）
    props = fetchProps(ME.user, "open_grid_add");
    for _, prop in ipairs(props) do
        trigger(ME.user, prop[1], prop[2], para.pos);
    end

    -- 专属技，开格子加闪避
    props = PropM.fetchProps(ME.user, "sneak");
    for _, prop in pairs(props) do
        PropM.trigger(ME.user, prop[1], prop[2], para.pos);
    end

    local grid = DungeonM.getCurrentDungeon()[para.pos];
    -- 触发伤害
    props = fetchProps(ME.user, "apper_damage");
    for _, prop in ipairs(props) do
        if grid.monster then
            trigger(ME.user, prop[1], prop[2], { grid.monster, grid:getPos(), });
        end
    end
    -- 触发中毒
    props = fetchProps(ME.user, "apper_poisoned");
    for _, prop in ipairs(props) do
        trigger(ME.user, prop[1], prop[2], grid);
    end
    -- 触发眩晕
    props = fetchProps(ME.user, "apper_spin");
    for _, prop in ipairs(props) do
        trigger(ME.user, prop[1], prop[2], grid);
    end
    -- 触发石化
    props = fetchProps(ME.user, "apper_petrify");
    for _, prop in ipairs(props) do
        trigger(ME.user, prop[1], prop[2], grid);
    end
    -- 触发冰冻
    props = fetchProps(ME.user, "appear_frozen");
    for _, prop in ipairs(props) do
        trigger(ME.user, prop[1], prop[2], grid);
    end

    -- 翻开格子收集冈布奥技能能量
    props = fetchProps(ME.user, "open_gain_attrib");
    for _, prop in ipairs(props) do
        trigger(ME.user, prop[1], prop[2]);
    end

    AssistMonsterM.openGridHandle(para);
end

-- 打开格子事件
EventMgr.register("prop_stat/open_grid", event.OPEN_GRID, handle);
EventMgr.register("prop_stat/open_grid", event.PURE_OPEN_GRID, handle);
